<?php
/**
 * User: hjd-yao
 * Date: 2018/11/5
 * Time: 15:03
 */

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\BackendController;
use App\Models\Shop\OrderModel;
use App\Models\Store\ShopStoreModel;
use App\Models\Store\ShopStoreWdModel;
use App\Models\User\RechargeRecordModel;
use App\Models\User\WdModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserBaseModel;
use Illuminate\Http\Request;
use App\Models\Shop\UpgradeModel;

class IndexController extends BackendController
{
    /**
     * @title 首页
     * @return View
     */
    public function index()
    {
        return view('backend::index.index',[
            'page_title'=>'首页'
        ]);
    }


    /**
     * @title 首页统计信息
     * @param Request $request
     * @return JsonResponse
     */
    public function statisticalInfo(Request $request)
    {
        $start_time = Carbon::today()->timestamp;
        $emd_time = Carbon::now()->timestamp;
        //dd($emd_time);
        $new_user = UserBaseModel::whereBetween('created_at',[$start_time,$emd_time])->where('status','>=',1)->count();

        $rg_apply = RechargeRecordModel::where('status',0)->count();
        $no_order = OrderModel::where('store_id',0)->where('status',1)->count();
        $week_sales = OrderModel::where('store_id',0)->where('status','>=',1)->whereIn('goods_zone',[1,2,3])
            ->whereBetween('created_at',[$start_time,$emd_time])->count();
        $wd_apply = WdModel::where('status',0)->count();
        $up_apply = UpgradeModel::init()->where('status',1)->count();
        return response()->json([
            'status'=>1,
            'data'=>[
                'no_order'=>$no_order,
                'new_users'=>$new_user,
                'rg_apply'=>$rg_apply,
                'wd_apply'=>$wd_apply,
                'week_sales'=>$week_sales,
                'up_apply'=>$up_apply
            ]
        ]);
    }

    /**
     * @title 会员趋势图
     * @return JsonResponse
     */
    public function chartUserData()
    {
        $list = UserBaseModel::where('status','>=',0)
            ->where('created_at','>=',Carbon::today()->subDay(6)->timestamp)
            ->selectRaw('count(*) as num,UNIX_TIMESTAMP(FROM_UNIXTIME(created_at,"%Y-%m-%d")) as dates')
            ->groupBy('dates')->get();

        $data = array_column($list->toArray(),'num','dates');

        $users = [];
        $dates = [];
        for($i=0; $i < 7; $i++){
            $date = Carbon::today()->subDay(6-$i);
            $dates[] = $date->format('Y/m/d');
            $users[] = $data[$date->timestamp] ?? 0 ;

        }
        return response()->json([
            'status'=>1,
            'data'=>[
                'dates'=>$dates,
                'users'=>$users,
            ]
        ]);
    }

    /**
     * @title 订单趋势图
     * @return JsonResponse
     */
    public function chartOrderData()
    {
        $list = OrderModel::where('status','>=',1)
            ->where('created_at','>=',Carbon::today()->subDay(6)->timestamp)
            ->selectRaw('count(*) as num,sum(goods_price) as sales_num,UNIX_TIMESTAMP(FROM_UNIXTIME(created_at,"%Y-%m-%d")) as dates')
            ->groupBy('dates')->get();

        $num = array_column($list->toArray(),'num','dates');
        $sales_money = array_column($list->toArray(),'sales_num','dates');
        //dd($data);
        $orders = [];
        $sales = [];
        $dates = [];
        for($i=0; $i < 7; $i++){
            $date = Carbon::today()->subDay(6-$i);
            $dates[] = $date->format('Y/m/d');
            $orders[] = $num[$date->timestamp] ?? 0;
            $sales[] = $sales_money[$date->timestamp] ?? 0;

        }
        return response()->json([
            'status'=>1,
            'data'=>[
                'dates'=>$dates,
                'orders'=>$orders,
                'sales'=>$sales,
            ]
        ]);
    }

}